/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
using System;
using System.IO;
using Madason.MetaModel;
using System.Collections.Generic;
using Madason.MetaModel.Interfaces;
using System.Xml;
using Madason.MetaModel.Validation;
using System.Reflection;
using Madason.GenUtils;
using Madason.MetaModel.DAO;

/// <summary>
/// Summary description for Program
/// </summary>
public class Program
{
	private Madason.MetaModel.Model model;


	/// <summary>
	/// the current model
	/// </summary>
	public Madason.MetaModel.Model Model
	{
		get { return model; }
		set { model = value; }
	}


		/// <summary>
	/// generate data access objects
	/// </summary>
	/// <param name="classes"></param>
	/// <param name="argMap"></param>
	public static void GenerateGui(Madason.MetaModel.ClassList classes, Namespace pageNamespace, System.Collections.Generic.Dictionary<string, string> argMap)
	{
		string webroot = argMap["webroot"];
		string javasrc = argMap["javasrc"];
		string pagePackage = argMap["pagePackage"];

		foreach (Class cls in classes)
		{
			Madason.MetaModel.Namespace ns = new Madason.MetaModel.Namespace();
			ns.Name = pagePackage;
			
			string path = ns.CreateDirectory(javasrc).FullName + "\\" + cls.Name + "ListPage.java";
			using (SensorWriter writer = new SensorWriter(path))
			{
				Console.WriteLine("  generating " + path + "...");
				ListPageClass clsTemplate = new ListPageClass();
				clsTemplate.Class = (Entity)cls;
				clsTemplate.Package = pagePackage;
				clsTemplate.Render(writer);
			}

			path = webroot + "\\" + cls.NameCamelCase + "List.htm";
			using (SensorWriter writer = new SensorWriter(path))
			{
				Console.WriteLine("  generating " + path + "...");
				ListPageHtm htmTemplate = new ListPageHtm();
				htmTemplate.Class = (Entity)cls;
				htmTemplate.Render(writer);
			}

			//
			// edit pages
			//

			path = ns.CreateDirectory(javasrc).FullName + "\\" + cls.Name + "EditPage.java";
			using (SensorWriter writer = new SensorWriter(path))
			{
				Console.WriteLine("  generating " + path + "...");
				EditPageClass clsTemplate = new EditPageClass();
				clsTemplate.Class = (Entity)cls;
				clsTemplate.Package = pagePackage;
				clsTemplate.Render(writer);
			}

			path = webroot + "\\" + cls.NameCamelCase + "Edit.htm";
			using (SensorWriter writer = new SensorWriter(path))
			{
				Console.WriteLine("  generating " + path + "...");
				EditPageHtm htmTemplate = new EditPageHtm();
				htmTemplate.Class = (Entity)cls;
				htmTemplate.Render(writer);
			}
		}
	}


	/// <summary>
	/// main function
	/// - parse command argumends
	/// - invoke special generator-functions
	/// </summary>
	/// <param name="args"></param>
	[STAThread]
	public static int Main(string[] args)
	{
		GeneratorWorkflow workflow = new GeneratorWorkflow(args);
		Context.TargetLanguage = TargetLanguages.Java;
		
		workflow.AddParameterDefinition("webroot", ParameterType.Directory, "Path of the web application", true);
		workflow.AddParameterDefinition("javasrc", ParameterType.Directory, "Path of the java sources", true);
		workflow.AddParameterDefinition("pagePackage", ParameterType.Directory, "Package to place the page classes", true);
		workflow.CheckCommandLine();
		workflow.LoadModel();
		workflow.Transform();
		workflow.Validate();


		//
		// Generate sources
		//
		Namespace pageNamespace = workflow.Model.CreateNamespace(workflow.Arguments["pagePackage"]);
		ClassList classesToGenerate = workflow.Model.DeepGetClasses().Filter(Stereotype.Entity);
		GenerateGui(classesToGenerate, pageNamespace, workflow.Arguments);
		
		return 0;
	}
}
